﻿#region Copyright (c) 2010 R. Uittenbosch
/*
* Copyright (C) 2010 R. Uittenbosch
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
* associated documentation files (the "Software"), to deal in the Software without restriction, including 
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
* following conditions:
* 
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.ComponentModel;
using TouchUI.System.Mobile.Mvc;
using TouchUI;
namespace LinkedIn.Mobile
{

  /// <summary>
  /// The bottom actionmenu.
  /// </summary>
  public class ActionMenu : TouchUI.Control, ISupportInitialize
  {
    private int CurrentBtn = -1;
    private int SelectedBtn = -1;
    public event EventHandler<ExecuteCommandEventArgs> Execute;
    private Bitmap _orgImage;
    private Bitmap _selImage;
    private Bitmap _paintImage;
    private int _commandWidth;
    private int _orgCommandWidth;
    public Rectangle Bounds {get; private set;}
    private bool handleMouseUp = false;
   
    public int ImageHeight { get; private set; }

    /// <summary>
    /// Constructor
    /// </summary>
    public ActionMenu()
    {
      ScreenFactor = Common.Instance.ScreenFactor;
      Rectangle = Screen.PrimaryScreen.WorkingArea;
      _orgImage = LinkedIn.Mobile.Properties.Resources.nbars;
      _selImage = LinkedIn.Mobile.Properties.Resources.nbars;
      _commandWidth = Rectangle.Width;
      //image 0 x 600
      // bar 0 x 480
      // 320/600 * 480
      _commandWidth = 320;
      _orgCommandWidth = 320;
      // determine the boxes in the bottom row
      float f = (((float)_commandWidth / (float)_orgImage.Size.Width) * (float)Rectangle.Width);

      Common.Instance.AdjustBottomHeigth = Rectangle.Width * _orgImage.Size.Height / _orgImage.Size.Width;
      _commandWidth = (int)f;
      _paintImage = _orgImage;
      // The current size of the boz.
      Bounds = new Rectangle(Rectangle.Left, Rectangle.Height - Common.Instance.AdjustBottomHeigth, Rectangle.Width, Common.Instance.AdjustBottomHeigth);
    }

    public string[] Commands { private get; set; }
    public override void Paint(Graphics g)
    {
      // Draw the requires image
      g.DrawImage
        (_paintImage,
        new Rectangle(Rectangle.Left, Rectangle.Height - Common.Instance.AdjustBottomHeigth, Rectangle.Width, Common.Instance.AdjustBottomHeigth),
        new Rectangle(0, 0, _paintImage.Width, _paintImage.Height), GraphicsUnit.Pixel);    
    }

    public override void MouseDown(Point p)
    {
      handleMouseUp = true;
    }
    public override void MouseMove(Point p)
    {
      // What is the current button....
      int div = _commandWidth / Commands.Length;
        CurrentBtn = (int)(p.X / div);
        if (CurrentBtn > Commands.Length)
          CurrentBtn = SelectedBtn;
        System.Diagnostics.Debug.WriteLine(string.Format("x{0}, div{1}, width{2}, btn{3}", p.X, div, Rectangle.Width, CurrentBtn));
   
    }

    public override void MouseUp(Point p)
    {
      // Did we have a mousedown?
      if (!handleMouseUp)
        return;
      // Did it occure on a button?
      if (CurrentBtn == -1)
        return;

      SelectedBtn = CurrentBtn;
      // Create a new image based on the selected button
      _paintImage = Create(_orgImage, _selImage);

      // Execute the command associated.
      // if (Execute != null)
      //  Execute(this, new ExecuteCommandEventArgs() { CommandText = Commands[SelectedBtn] });
      handleMouseUp = false;
    }

    /// <summary>
    /// Create a new bitmap from 2 bitmaps, 1 selected, 1 not selected
    /// </summary>
    /// <param name="srcBitmap"></param>
    /// <param name="selBitmap"></param>
    /// <returns></returns>
    private Bitmap Create(Bitmap srcBitmap, Bitmap selBitmap)
    {

      // Create the new bitmap and associated graphics object
      Bitmap newbmp = new Bitmap(srcBitmap.Width, srcBitmap.Height);

      Graphics g = Graphics.FromImage(newbmp);
      // copy entir
      g.DrawImage(srcBitmap, 0, 0, new Rectangle(0,0,srcBitmap.Width, srcBitmap.Height), GraphicsUnit.Pixel);
      int div = _orgCommandWidth / Commands.Length;
      Rectangle r = new Rectangle(div * SelectedBtn, 0, div, srcBitmap.Height);
      g.DrawImage(selBitmap, div * SelectedBtn,0, r, GraphicsUnit.Pixel);
      // Clean up
      g.Dispose();

      // Return the bitmap
      return newbmp;
    }


    #region ISupportInitialize Members

    public void BeginInit()
    {
      //base.BeginInit();
      
    }

    public void EndInit()
    {
      
    }

    #endregion
  }

  public class ExecuteCommandEventArgs : EventArgs
  {
    public string CommandText {get; set;}
  }
}
